package com.xlh.dto.courseTest;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.xlh.dto.course.CourseBasicDTO;
import com.xlh.dto.user.UserInfoDTO;
import com.xlh.pojo.courseTest.*;
import com.xlh.util.BeanUtil;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author zhr 2019/3/23
 */
@Data
public class PaperReadingDTO {
    // 答题卡id
    private Long id;
    // 试卷总分数
    private Integer score;
    private List<TypeReadingDTO> readingList;

    // 实验报告相似度设置
    private Boolean checkReportDuplicate;
    // 实验报告相似度警告百分比
    private Integer reportDuplicatePercent;

    // 学号
    private String userNumber;
    // 姓名
    private String userName;
    // 试卷名称
    private String paperName;
    // 课程名称
    private String courseName;

    // 上一张试卷答题卡id
    private Long preview;
    // 下一张试卷答题卡id
    private Long next;

    public PaperReadingDTO convert(
            CourseTestPaperAnswerSheet answerSheet, List<CourseTestPaperQuestion> questionList,
            List<CourseTestPaperAnswer> answerList, List<CourseTestPaperAnswerSheet> answerSheetList,
            CourseTestPaper paper, CourseBasicDTO course, UserInfoDTO user, Map<Integer, Integer> typeScoreMap,
            List<AnswerSheetDTO> answerSheetDTOList, Map<Long, List<DuplicateResultDTO>> questionDuplicateMap,
            Map<Long, UserPaperExperimentReportDTO> idExperimentMap
    ) {
        // 设置答题卡id，当前总得分
        if (answerSheet != null) {
            this.setId(answerSheet.getId());
            this.setScore(answerSheet.getScore());
        }
        if (course != null) {
            this.setCourseName(course.getName());
        }
        if (paper != null) {
            this.setPaperName(paper.getPaperName());
            this.setCheckReportDuplicate(paper.getCheckReportDuplicate());
            this.setReportDuplicatePercent(paper.getReportDuplicatePercent());
        }
        if (user != null) {
            this.setUserName(user.getName());
            this.setUserNumber(user.getUserNumber());
        }
        if (CollectionUtils.isNotEmpty(answerSheetList) && answerSheet != null) {
            // 设置上一题,下一题
            for (int i = 0; i < answerSheetList.size(); i++) {
                if (Objects.equal(answerSheet.getId(), answerSheetList.get(i).getId())) {
                    if (i - 1 >= 0) this.setPreview(answerSheetList.get(i - 1).getId());
                    if (i + 1 < answerSheetList.size()) this.setNext(answerSheetList.get(i + 1).getId());
                    break;
                }
            }
        }

        Map<Long, AnswerSheetDTO> questionIdUserAnswerMap = BeanUtil.mapByKey("id", answerSheetDTOList);
        Map<Long, List<CourseTestPaperAnswer>> questionIdAnswerListMap = BeanUtil.aggByKeyToList("questionId", answerList);
        List<ReadingDTO> readings = BeanUtil.batchTransform(ReadingDTO.class, questionList);
        for (ReadingDTO reading : readings) {
            reading.convert(questionIdUserAnswerMap, questionIdAnswerListMap, questionDuplicateMap, idExperimentMap);
        }

        // 将readingList转为typeReadingList
        Map<Integer, List<ReadingDTO>> typeReadingListMap = BeanUtil.aggByKeyToList("type", readings);
        List<TypeReadingDTO> typeReadingList = Lists.newArrayList();
        typeReadingListMap.forEach((type, list) -> typeReadingList.add(new TypeReadingDTO().convert(type, list, typeScoreMap)));

        // 设置typeReadingList
        this.setReadingList(typeReadingList);

        return this;
    }


    /*public Map<Long, List<Map<String, String>>> convert() {

        Map<Long, List<Map<String, String>>> map = new HashMap<>();
        // 组装头文件
        Long code = Long.valueOf(QuestionTemplateTypeEnum.HEADER.getCode());
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> typeMap = new HashMap<>();
        typeMap.put("userNumber", userNumber);
        typeMap.put("userName", userName);
        typeMap.put("paperName", paperName);
        typeMap.put("courseName", courseName);
        list.add(typeMap);
        map.put(code, list);
        // 组装题目
        for (TypeReadingDTO typeReadingDTO : this.readingList) {

            Long qCode = Long.valueOf(typeReadingDTO.getType());
            List<ReadingDTO> questionList = typeReadingDTO.getQuestionList();
            List<Map<String, String>> qlist = new ArrayList<>();
            for (ReadingDTO readingDTO : questionList) {

                Map<String, String> qtypeMap = new HashMap<>();
                qtypeMap.put("orderNumber", readingDTO.getContent());
                qtypeMap.put("content", readingDTO.getContent());
                qtypeMap.put("userAnswer", (String)readingDTO.getUserAnswer());
                qtypeMap.put("score", String.valueOf(readingDTO.getScore()));
                qtypeMap.put("right", String.valueOf(readingDTO.getRight()));
                qlist.add(qtypeMap);
            }
            map.put(qCode, qlist);
        }
        // 组装结尾评论
        Long commentCode = Long.valueOf(QuestionTemplateTypeEnum.COMMENT.getCode());
        List<Map<String, String>> commentList = new ArrayList<>();
        Map<String, String> commentMap = new HashMap<>();
        commentMap.put("comment", "预留评论字段");
        commentList.add(typeMap);
        map.put(commentCode, commentList);

        return map;
    }*/
}
